package com.example.JcGQS.schedule;

import com.example.JcGQS.dao.PurchaseDao;
import com.example.JcGQS.enums.PurchaseStateEnum;
import com.example.JcGQS.pojo.entity.Purchase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 采购
 */
@Slf4j
@Component
public class PurchaseSchedule {
@Autowired
    PurchaseDao purchaseDao;
    /**
     * 清除死数据(修改时间超过十天就认为是死数据)
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void clearingData() {
        List<Purchase> purchases = purchaseDao.PurchaseList();
        for (Purchase purchase : purchases) {
            //采购状态为审核中
            if (purchase.getPurchaseState().equals(PurchaseStateEnum.REVIEWEDING.getValue())){
            // 获取当前时间减去10天后的时间
            LocalDateTime tenDaysAgo = LocalDateTime.now().minusDays(10);

// 比较当前时间和purchase.getDataUpdateTime()之间的差异
            if (tenDaysAgo.compareTo(purchase.getDataUpdateTime()) > 0) {
                // 当前时间减去10天后的时间晚于 purchase.getDataUpdateTime()，执行相关操作
                //变为审核失败
                purchase.setPurchaseState(PurchaseStateEnum.NOT_ALLOW.getValue());
                purchase.setPurchaseRemark(purchase.getPurchaseRemark()+"(超过十天未审核成功,默认变为审核失败)");
                purchaseDao.updatePurchase(purchase);
            }
            }
        }
    }
}
